angular.module('vip-list', ['swipe-list','head-toast'])

.controller 'VipListCtrl', ($scope, $state, $http,$localStorage, $ionicHistory, $stateParams, newAddress, wfCache,$ionicViewSwitcher,$ionicPopup,userData,paginationFactory,$cordovaSms,showToast) ->
    console.log userData.getToken()
    paginationSrv = new paginationFactory('VipListCtrl','/shopuser/')
    unWatch = paginationSrv.watch (date) ->
        $scope.vipList = []
        _.each date,(item)->
            vip = {
                id:item.id
                username:item.username
#                txt2:item.islogin
                account:item.account
                overdraft:item.overdraft
                usertypeid: item.usertypeid
                usertypename: item.usertypename
                remarks:item.remarks
            }
            $scope.vipList.push vip
        $scope.userTypeList = []
        _.each date,(item)->
            userType = {
                typeid:item.typeid
                typename:item.typename
            }
            $scope.userTypeList.push userType

    requestTypeList =() ->
        $http.get("/shopuser/",params:{action:'userlist',userid:userData.getToken()})
        .success (resp) ->
            if resp.status == '0'
                $scope.userTypeList = resp.usertypelist
                $scope.TypeList = []
                _.each $scope.userTypeList,(item)->
                    userType = {
                        typeid:item.typeid
                        typename:item.typename
                    }
                    $scope.TypeList.push userType
                console.log $scope.TypeList
            else
                showToast '获取会员类型失败'
        .error ->
            showToast '网络调皮了'

    paginationSrv.reload(true, {
        action:'userlist'
        userid: userData.getToken()
    })
    $scope.sendAllMessage = false
    _.extend $scope,
        input:
            phoneNumber:undefined
        canLoadMore: -> paginationSrv.canLoadMore()
        doRefresh: ->
            paginationSrv.reload(true).finally -> $scope.$broadcast 'scroll.refreshComplete'
        loadMore: ->
            paginationSrv.loadMore().finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'
        goBack: ->
          $ionicViewSwitcher.nextDirection('back')
          $ionicHistory.goBack()
        pick: (i) ->
            console.log 'pick', i
        sendMessage: (item)->
            console.log 'item.txt1',item.username
            options = {
                replaceLineBreaks: false
                android: {
                    intent: 'INTENT'
                }
            }
            $cordovaSms
            .send(item.username,'',options)
        editRemark:(item)->
            $ionicPopup.prompt({
                title:'输入新的备注'
                okText:'编辑'
                cancelText:'取消'
            }).then (res) ->
                if res
                    $scope.changeRemark(item,res)
        changeRemark:(item,remark)->
            console.log remark,item.id
            $http.post('/utf-8data/',{action:'setuserbak',shopid:userData.getToken(),userid:item.id,remarks:remark})
            .success (resp) ->
                console.log resp
                if resp.status=='0'
                    item.remarks=remark
                    showToast '修改会员备注成功'
                else
                    showToast resp.msg
            .error (err) ->
                console.log err
                showToast '修改会员备注失败'
        sendAll:() ->
            $scope.sendAllMessage =! $scope.sendAllMessage
        cancelOption: ->
            $scope.sendAllMessage = false
        confirmSend: ->
            vipsToSend = []
            _.each $scope.vipList,(item)->
                if item.isCheckIn == true
                    vipsToSend.push item.username
            console.log vipsToSend
            options = {
                replaceLineBreaks: false
                android: {
                    intent: 'INTENT'
                }
            }
            $cordovaSms
            .send(vipsToSend,'',options)

        revise:(i) ->
            alert($scope.vipList[i].username)
        goAccount:(item) ->
#            $localStorage.vipItem=item #ios出现参数传递后丢失的bug,暂时改为storage
            $state.go 'tab.increase-account-home',{vipItem:item}
        goReset:(item) ->
#            $localStorage.vipItem=item
            $state.go 'tab.reset-overdraft-home',{vipItem:item}

        goSearch: ->
            paginationSrv.reload(true, {
                action:'userlist'
                userid: userData.getToken()
                keywords:$scope.input.phoneNumber
            })

        clear: ->
            $scope.input.phoneNumber = undefined

        changeType: (item) ->
            $http.get("/shopuser/",params:{action:'changeusertype',shopid:userData.getToken(),userid:item.id,typeid:item.usertypeid})
            .success (resp) ->
                if resp.status == '0'
                    showToast '修改会员类型成功!'
                else
                    showToast '修改会员类型失败!'
            .error ->
                showToast '您的网络又调皮了'
        remove: (i) ->
            console.log 'remove',i
            $http.post("/shopuser/",{action:'deluser',userid:userData.getToken(),id:$scope.vipList[i].id})
            .success (resp) ->
                if resp?.status == '0'
                    $scope.vipList.splice(i,1)
                    showToast '删除成功'
                else
                    console.log '失败'
            .error ->
                showToast '您的网络又调皮了'
    $scope.$on '$destory', ->
        unWatch()

    $scope.$on '$ionicView.enter',->
        requestTypeList()
        paginationSrv.reload(true, {
            action:'userlist'
            userid: userData.getToken()
        })

